/**
 * 通过 mutation 间接更新 state 的多个方法的对象
 */

import { RECEIVE_ADDRESS, RECEIVE_CATEGORYS, RECEIVE_SHOPS, RECEIVE_USER_INFO, RESET_USER_INFO, RECEIVE_RATINGS, RECEIVE_INFO, RECEIVE_GOODS, INCREMENT_FOOD_COUNT, DECREMENT_FOOD_COUNT, CLEAR_CART, RECEIVE_SEARCH_SHOPS  } from './mutation-type.js'
import { reqAddress, reqFoodCategory, reqShops, reqUserInfo, reqLogout, reqShopGoods, reqShopInfo, reqShopRatings, reqSearchShop } from '../api/index.js'

export default {
  // 异步获取地址
  async getAddress ({commit, state}) {
    // 发送异步 ajax 的请求
    const geohash = state.latitude + ',' + state.longitude
    const result = await reqAddress(geohash)
    // 提交一个 mutation
    if (result.code === 0) {
      const address = result.data
      commit(RECEIVE_ADDRESS, {address})
    }
  },

  // 异步获取食品分类列表
  async getCategorys ({commit, state}) {
    // 发送异步 ajax 的请求
    const result = await reqFoodCategory()
    // 提交一个 mutation
    if (result.code === 0) {
      const categorys = result.data
      commit(RECEIVE_CATEGORYS, {categorys})
    }
  },

  // 异步获取商家列表
  async getShops ({commit, state}) {
    // 发送异步 ajax 的请求
    const { latitude, longitude} = state
    const result = await reqShops(longitude, latitude)
    // 提交一个 mutation
    if (result.code === 0) {
      const shops = result.data
      commit(RECEIVE_SHOPS, {shops})
    }
  },

  // 同步记录用户信息
  recordUser ({commit, state}, userInfo) {
    commit(RECEIVE_USER_INFO, {userInfo})
  },

  // 异步获取用户信息
  async getUserInfo ({commit, state}) {
    // 发送异步 ajax 的请求
    const result = await reqUserInfo()
    // 提交一个 mutation
    if (result.code === 0) {
      const userInfo = result.data
      commit(RECEIVE_USER_INFO, {userInfo})
    }
  },
  
  // 用户登出
  async logout ({commit, state}) {
    // 发送异步 ajax 的请求
    const result = await reqLogout()
    // 提交一个 mutation
    if (result.code === 0) {
      commit(RESET_USER_INFO)
    }
  },

  // 异步获取商家信息
  async getShopInfo ({commit, state}) {
    const result = await reqShopInfo()
    if (result.code === 0) {
      const info = result.data
      commit(RECEIVE_INFO, {info})
    }
  },

  // 异步获取商家评价列表
  async getShopRatings ({commit, state}, callback) {
    const result = await reqShopRatings()
    if (result.code === 0) {
      const ratings = result.data
      commit(RECEIVE_RATINGS, {ratings})
      // 数据更新了，通知一下组件
      callback && callback()
    }
  },

  // 异步获取商家商品列表
  async getShopGoods ({commit, state}, callback) {
    const result = await reqShopGoods()
    if (result.code === 0) {
      const goods = result.data
      commit(RECEIVE_GOODS, {goods})
      // 数据更新了，通知一下组件
      callback && callback()
    }
  },

  // 修改购物车数量
  updateFoodCount ({commit, state}, {isAdd, food}) {
    if (isAdd) {
      commit(INCREMENT_FOOD_COUNT, {food}) // true 说明是商品新增操作
    } else {
      commit(DECREMENT_FOOD_COUNT, {food}) // false 说明是商品减少操作
    }
  },

  // 清空购物车
  clearCart ({commit, state}) {
    commit(CLEAR_CART)
  },
  
  // 异步获取搜索商品列表
  async getSearchShops ({commit, state}, {keyword}) {
    const geohash = state.latitude + ',' + state.longitude
    const result = await reqSearchShop(geohash, keyword)
    if (result.code === 0) {
      const searchShops = result.data
      commit(RECEIVE_SEARCH_SHOPS, {searchShops} )
    }
  }
}
